package com.example.springboot.service.utils;

import cn.hutool.core.date.DateUtil;
import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.example.springboot.common.Constants;
import com.example.springboot.common.enums.ResultCodeEnum;
import com.example.springboot.common.enums.RoleEnum;
import com.example.springboot.entity.Account;
import com.example.springboot.exception.CustomException;
import com.example.springboot.service.AdminService;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.Date;

/**
 * 功能:
 * 作者: whs
 * 日期:2024-11-19 10:20:00
 */

/**
 * token工具类
 */
@Component
public class TokenUtils {
    private static final Logger log = LoggerFactory.getLogger(TokenUtils.class);

    @Resource
    private AdminService adminService;

    private static AdminService staticAdminService;

    @PostConstruct
    public void init(){
        staticAdminService=adminService;
    }
    /**
     *生成JWTtoken
     */
    public static String createToken(String data,String sign){
        //Audience 是存储数据的一个媒介 存储用户ID和用户的角色 1-ADMIN
        return JWT.create().withAudience(data)
                .withExpiresAt(DateUtil.offsetDay(new Date(),1))  //设置过期时间1天后
                .sign(Algorithm.HMAC256(sign));
    }
    /**
     * 获取当前登录的用户
     */
    public static Account getCurrentUser(){
        try {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            String token=request.getHeader(Constants.TOEKN);
            String audience = JWT.decode(token).getAudience().get(0);
            String[] userRole=audience.split("-");
            Integer userId= Integer.valueOf(userRole[0]);
            String role=userRole[1];
            if(RoleEnum.ADMIN.name().equals(role)){
                return staticAdminService.selectById(userId);
            }
        }catch(Exception e){
            log.error("获取当前登录用户出错",e);
        }
        return null;
    }
}